﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IBupro.Classes;
using IBupro.Controllers.Classes;
using iBuproModel;

namespace IBupro.Controllers
{
    public class ProjectSourceController : CustomController
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">id of Student Project</param>
        /// <returns></returns>
        public ActionResult ListSources(int id)
        {
            var sources = repositoryService.ProjectSourceRepositoryCreateInstance();
            IEnumerable<ProjectSource> list = sources.GetAll(s => s.Project.Id == id);
            return View(list);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">Student project id</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult Create(int id)
        {
            var user = users.GetUserByUserName(User.Identity.Name);
            var studentProjects = repositoryService.ProjectRepositoryCreateInstance();
            var project = studentProjects.First(p => p.Id == id);
            if (!project.Users.Contains(user) && project.Principal != user)
            {
                FlashMessenger.AddMessage("You cannot modify this project source", MessageType.Error);
                return RedirectToAction("Index", "Home");
            }
            ProjectSource source = new ProjectSource();
            return View(source);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">Student project id</param>
        /// <param name="source">Project source</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult Create(int id, ProjectSource source)
        {

            
            var sources = repositoryService.ProjectSourceRepositoryCreateInstance();
            var studentProjects = repositoryService.ProjectRepositoryCreateInstance();
            var project = studentProjects.First(p => p.Id == id);
            var users = repositoryService.UserRepositoryCreateInstance();
            var user = users.GetUserByUserName(User.Identity.Name);

            if (!project.Users.Contains(user) && project.Principal != user)
            {
                FlashMessenger.AddMessage("You cannot modify this project source",MessageType.Error);
                return RedirectToAction("Index", "Home");
            }

            source.Project = project;
            sources.Add(source);
            sources.SaveChanges();

            return RedirectToAction("Detail", "StudentProject", new {id = id});
        }

        public ActionResult Delete(int id, int projectId)
        {
            var sources = repositoryService.ProjectSourceRepositoryCreateInstance();
            var studentProjects = repositoryService.ProjectRepositoryCreateInstance();
            var project = studentProjects.First(p => p.Id == projectId);
            var users = repositoryService.UserRepositoryCreateInstance();
            var user = users.GetUserByUserName(User.Identity.Name);
            var source = sources.First(s => s.Id == id);

            if (!project.Users.Contains(user) && project.Principal != user)
            {
                FlashMessenger.AddMessage("You cannot modify this project source", MessageType.Error);
                return RedirectToAction("Index", "Home");
            }

            sources.Delete(source);
            sources.SaveChanges();

            return RedirectToAction("Detail", "StudentProject",new {id = project.Id});
        }





    }
}
